//
//  Language.m
//  Finder
//
//  Created by cdm on 11-4-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "AppLanguage.h"

@implementation AppLanguage

+ (AppLanguageType)getLanguage
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    if ([preferredLang isEqualToString:@"zh-Hant"]) {
        return AppLanguageZhHant;
    } else {
        return AppLanguageZhHans;
    }
}


+ (NSString *)getLanguageAsString
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    if ([preferredLang isEqualToString:@"zh-Hant"]) {
        return @"zh-Hant";
    } else {
        return @"zh-Hans";
    }
}

+ (void)setLanguage:(AppLanguageType)language;
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = nil;
  
    if (language == AppLanguageZhHant) {
        languages = [NSArray arrayWithObject:@"zh-Hant"];
    } else {
        languages = [NSArray arrayWithObject:@"zh-Hans"];
    }

    [defs setObject:languages forKey:@"AppleLanguages"];
    [defs synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kAppLanguageChangedNotification object:nil];
}

+ (NSString *)getString:(NSString *)key
{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"AppLanguage" ofType:@"plist" inDirectory:nil forLocalization:[AppLanguage getLanguageAsString]];
    
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSString *value = [dict objectForKey:key];
    return value;
}


@end
